package com.aijia.custom.product.manager;

import android.content.Context;

import com.aijia.custom.product.http.Batch;
import com.aijia.custom.product.http.Command;
import com.aijia.custom.product.http.HttpUtil;
import com.aijia.custom.product.listener.IModifyPlanDate;
import com.aijia.custom.product.listener.IModifyPlanDateListener;
import com.aijia.custom.product.logical.Action;
import com.aijia.custom.product.utils.SharedPreferencesUtils;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;

/**
 * Created by yejianjun on 2019/2/19.
 */

public class ModifyPlanDateManager implements IModifyPlanDate {
    private IModifyPlanDateListener mListener = null;
    private Context mContext;

    public ModifyPlanDateManager(Context ctx) {
        mContext = ctx;
    }

    @Override
    public void modifyPlanDate(int workSheetId, String planFinishDate) {
        // 向平台发送登录请求
        StringEntity stringEntity = null;
        try {
            Batch batch = new Batch();
            batch.setToken(SharedPreferencesUtils.getToken(mContext));
            Command command = new Command();
            command.addAction(Action.ACTION_WORKSHEET_MODIFY_PLANDATE)
                    .addArgs("workSheetId", workSheetId)
                    .addArgs("planFinishDate", planFinishDate);
            batch.addCommand(command);
            stringEntity = new StringEntity(batch.toJson(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        HttpUtil http = new HttpUtil();
        http.req(mContext, stringEntity, false, new HttpUtil.MyResponse() {

            @Override
            public void responseSucess(Object obj) {
                // TODO Auto-generated method stub
                // 通知界面
                if (null != mListener)
                    mListener.onModifyPlanDateSuccess();
            }

            @Override
            public void responseFailure(int errorCode, String msg) {
                // TODO Auto-generated method stub
                if (null != mListener)
                    mListener.onModifyPlanDateFailure(errorCode, msg);
            }
        });
    }

    @Override
    public boolean addModifyPlanDateListener(IModifyPlanDateListener listener) {
        mListener = listener;
        return mListener != null;
    }

    @Override
    public boolean removeModifyPlanDateListener(IModifyPlanDateListener listener) {
        if (null == listener)
            return false;
        mListener = null;
        return true;
    }
}
